package com.xukai.springboot.controller.sys;

import com.xukai.springboot.entity.request.sys.DelMenuRequest;
import com.xukai.springboot.entity.request.sys.EditMenuStateRequest;
import com.xukai.springboot.entity.response.PageResult;
import com.xukai.springboot.entity.response.ResponseBase;
import com.xukai.springboot.entity.response.sys.MenuResponse;
import com.xukai.springboot.entity.sys.t_sys_authority;
import com.xukai.springboot.entity.sys.t_sys_user;
import com.xukai.springboot.service.sys.MenuService;
import com.xukai.springboot.util.session.SessionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(basePath = "api/menu", description = "权限管理")
@RestController
@RequestMapping("api/menu")
public class MenuController {

    /**
     * 日志
     */
    private final Logger logger = LoggerFactory.getLogger(MenuController.class);

    /**
     * 菜单服务
     */
    @Autowired
    private MenuService menuService;

    /**
     * session服务
     */
    @Autowired
    private SessionService sessionService;

    /**
     * 获取菜单列表
     *
     * @return
     */
    @ApiOperation(value = "获取菜单列表", notes = "获取菜单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentID", value = "父级ID，如果为顶级菜单默认值为0", required = true, dataType = "int", example = "0", defaultValue = "0", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "数据状态，如果要查询全部则传递-1", required = true, dataType = "int", example = "0", defaultValue = "0", paramType = "query")
    })
    @GetMapping("GetMenuList")
    public ResponseBase<List<MenuResponse>> GetMenuList(@RequestParam int parentID, @RequestParam int state) {
        ResponseBase<List<MenuResponse>> result = new ResponseBase<>();
        try {
            List<t_sys_authority> data = menuService.GetMenuList(parentID, state);
            if (data == null || data.size() == 0) {
                return result;
            }
            result.setData(new ArrayList<>());
            for (t_sys_authority item : data) {
                MenuResponse resultItem = new MenuResponse();
                resultItem.setId(item.getId());
                resultItem.setName(item.getName());
                resultItem.setPath(item.getPath());
                resultItem.setParentId(item.getParentId());
                resultItem.setSort(item.getSort());
                resultItem.setState(item.getState());
                resultItem.setShow(false);
                result.getData().add(resultItem);
                //获取子目录
                List<t_sys_authority> childData = menuService.GetMenuList(item.getId(), state);
                if (childData == null || childData.size() == 0) {
                    continue;
                }
                resultItem.setChild(new ArrayList<>());
                for (t_sys_authority childItem : childData) {
                    MenuResponse child = new MenuResponse();
                    child.setId(childItem.getId());
                    child.setName(childItem.getName());
                    child.setPath(childItem.getPath());
                    child.setParentId(childItem.getParentId());
                    child.setSort(childItem.getSort());
                    child.setState(childItem.getState());
                    child.setShow(false);
                    resultItem.getChild().add(child);
                }
            }
        } catch (Exception e) {
            logger.error("获取菜单列表", e);
            result.setStatue(0);
        }
        return result;
    }

    /**
     * 获取可访问菜单
     *
     * @return
     */
    @ApiOperation(value = "获取可访问菜单", notes = "获取可访问菜单")
    @ApiImplicitParams({

    })
    @GetMapping("GetMenuListByUserID")
    public ResponseBase<List<t_sys_authority>> GetMenuListByUserID() {
        ResponseBase<List<t_sys_authority>> result = new ResponseBase<>();
        try {
            //获取当前用户
            t_sys_user currentUser = (t_sys_user) sessionService.getCurrentUser();
            if (currentUser == null) {
                result.setStatue(0);
                result.setErrMessage("当前用户不存在");
                return result;
            }
            List<t_sys_authority> data = menuService.GetMenuListByUserID(0, currentUser.getId());
            if (data == null || data.size() == 0) {
                return result;
            }
            result.setData(new ArrayList<>());
            for (t_sys_authority item : data) {
                MenuResponse resultItem = new MenuResponse();
                resultItem.setId(item.getId());
                resultItem.setName(item.getName());
                resultItem.setPath(item.getPath());
                resultItem.setShow(false);
                result.getData().add(resultItem);
                //获取子目录
                List<t_sys_authority> childData = menuService.GetMenuList(resultItem.getId(), currentUser.getId());
                if (childData == null || childData.size() == 0) {
                    continue;
                }
                resultItem.setChild(new ArrayList<>());
                for (t_sys_authority childItem : childData) {
                    MenuResponse child = new MenuResponse();
                    child.setId(childItem.getId());
                    child.setName(childItem.getName());
                    child.setPath(childItem.getPath());
                    child.setShow(false);
                    resultItem.getChild().add(child);
                }
            }
        } catch (Exception e) {
            logger.error("获取菜单列表", e);
            result.setStatue(0);
        }
        return result;
    }


    /**
     * 获取目录详情
     */
    @ApiOperation(value = "获取目录详情", notes = "获取目录详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "目录ID", required = true, dataType = "int", example = "0", defaultValue = "0", paramType = "query")
    })
    @GetMapping("GetMenu")
    public ResponseBase<t_sys_authority> GetMenu(@RequestParam int id) {
        ResponseBase<t_sys_authority> result = new ResponseBase<>();
        try {
            result.setData(menuService.GetDetial(id));
        } catch (Exception e) {
            logger.error("获取目录详情", e);
            result.setStatue(0);
        }
        return result;
    }

    /**
     * 新建目录
     */
    @ApiOperation(value = "新建目录", notes = "新建目录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "param", value = "目录数据", required = true, dataType = "t_sys_authority", paramType = "body")
    })
    @PostMapping("AddMenu")
    public ResponseBase<t_sys_authority> AddMenu(@RequestBody t_sys_authority param) {
        ResponseBase<t_sys_authority> result = new ResponseBase<>();
        try {
            result.setData(menuService.AddMenu(param));
        } catch (Exception e) {
            logger.error("新建目录", e);
            result.setStatue(0);
        }
        return result;
    }

    /**
     * 编辑目录
     */
    @ApiOperation(value = "编辑目录", notes = "编辑目录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "param", value = "目录数据", required = true, dataType = "t_sys_authority", paramType = "body")
    })
    @PostMapping("EditMenu")
    public ResponseBase<t_sys_authority> EditMenu(@RequestBody t_sys_authority param) {
        ResponseBase<t_sys_authority> result = new ResponseBase<>();
        try {
            menuService.EditMenu(param);
        } catch (Exception e) {
            logger.error("编辑目录", e);
            result.setStatue(0);
        }
        return result;
    }

    /**
     * 修改目录状态
     */
    @ApiOperation(value = "修改目录状态", notes = "修改目录状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "param", value = "修改目录状态参数", required = true, dataType = "EditMenuStateRequest", paramType = "body")
    })
    @PostMapping("EditMenuState")
    public ResponseBase<t_sys_authority> EditMenuState(@RequestBody EditMenuStateRequest param) {
        ResponseBase<t_sys_authority> result = new ResponseBase<>();
        try {
            menuService.EditState(param.getId(), param.getState());
        } catch (Exception e) {
            logger.error("修改目录状态", e);
            result.setStatue(0);
        }
        return result;
    }

    /**
     * 判断目录名是否重复
     */
    /**
     * 获取目录详情
     */
    @ApiOperation(value = "判断目录名是否重复", notes = "判断目录名是否重复")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "目录ID,新建是传递-1；编辑时传递目录ID", required = true, dataType = "int", example = "0", defaultValue = "0", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "目录名称", required = true, dataType = "string", paramType = "query")
    })
    @GetMapping("IsExist")
    public ResponseBase<Boolean> IsExist(@RequestParam int id, @RequestParam String name) {
        ResponseBase<Boolean> result = new ResponseBase<>();
        try {
            result.setData(menuService.IsExist(id, name));
        } catch (Exception e) {
            logger.error("判断目录名是否重复", e);
            result.setStatue(0);
        }
        return result;
    }


    /**
     * 删除目录
     */
    @ApiOperation(value = "删除目录", notes = "删除目录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "param", value = "目录数据", required = true, dataType = "DelMenuRequest", paramType = "body")
    })
    @PostMapping("DelMenu")
    public ResponseBase<t_sys_authority> DelMenu(@RequestBody DelMenuRequest param) {
        ResponseBase<t_sys_authority> result = new ResponseBase<>();
        try {
            menuService.DelMenu(param.getId());
        } catch (Exception e) {
            logger.error("删除目录", e);
            result.setStatue(0);
        }
        return result;
    }

    /**
     * 获取顶级菜单
     *
     * @return
     */
    @ApiOperation(value = "获取顶级菜单", notes = "获取顶级菜单")
    @ApiImplicitParams({

    })
    @GetMapping("GetTopMenu")
    public ResponseBase<List<t_sys_authority>> GetTopMenu() {
        ResponseBase<List<t_sys_authority>> result = new ResponseBase<>();
        try {
            List<t_sys_authority> data = menuService.GetTopMenu();
            t_sys_authority parentItem = new t_sys_authority();
            parentItem.setName("根目录");
            data.add(0, parentItem);
            result.setData(data);
        } catch (Exception ex) {
            logger.error("获取顶级菜单", ex);
            result.setStatue(0);
        }
        return result;
    }

}
